<?php

// +----------------------------------------------------------------------
// | 文件 UserAuthEvent.class.php
// +----------------------------------------------------------------------
// | 说明 用户权限的内部接口
// +----------------------------------------------------------------------
// | 
// +----------------------------------------------------------------------
// | Author: 谢赤心 Legend. <xcx_legender@qq.com>
// +----------------------------------------------------------------------
namespace Api\Event;

use Api\Event\CacheEvent;

//  auth => level.   

class UserAuthEvent extends BaseEvent 
{

	static public $authlang = [
		'video'  => '发布视频',
		'voice'  => '修改语音签名',
		'avatar' => '修改头像',
		'background' => '修改背景',
		'comments' => '发布评论',
		'zan'      => '点赞',
	];

	// 检查用户的操作是否有权限
	static public function check($action, &$user)
	{
		$auths = self::getAuths();
		if (!$auths){
			return true;
		}
		return $user['level'] >= $auths[$action];
	}

	static public function getAuths()
	{
		$key = 'IMBA.AUTH.LEVEL';
		$auths = CacheEvent::hashGset(0, $key, function(){
			$auths = service('UserAuth')->select();
			$auths = array_column($auths, 'level', 'type');
			return $auths;
		});
		return $auths;
	}

	static public function resetCache()
	{
		$key = 'IMBA.AUTH.LEVEL';
		CacheEvent::remove(0, $key);
	}

}